define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'box/address/unit/index',
                    add_url: 'box/address/unit/add',
                    edit_url: 'box/address/unit/edit',
                    del_url: 'box/address/unit/del',
                    multi_url: 'box/address/unit/multi',
                    table: 'house',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'house_id',
                sortName: 'house_id',
                searchFormVisible: false,
                searchFormTemplate: 'customformtpl',
                columns: [
                    [
                        {field: 'house_id', title: __('House_id')},
                        {field: 'province_name', title: __('Province_name')},
                        {field: 'city_name', title: __('City_name')},
                        {field: 'area_name', title: __('Area_name')},
                        {field: 'quarters_name', title: __('Quarters_name')},
                        {field: 'house_name', title: __('House_name')},
                        {field: 'createtime', title: __('Createtime'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime},
                        //自定义栏位,custom是不存在的字段
                        {
                            field: 'custom',
                            title: __('操作'),
                            operate: false,
                            formatter: Controller.api.formatter.custom
                        }
                    ]
                ]
            });

            $(table).on("click", "[data-id].btn-delme", function (e) {
                e.preventDefault();
                var id = $(this).data("id");
                var that = this;
                Layer.confirm(
                        __('确定删除单元楼号吗？'),
                        {icon: 3, title: __('确定删除单元楼号吗？'), shadeClose: true},
                        function (index) {
                            $.post('box/address/unit/delme', {id: id}, function (res) {
                                table.bootstrapTable('refresh', {});
                            });
                            Layer.close(index);
                        }
                );
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {//渲染的方法
                custom: function (value, row, index) {
                    //添加上btn-change可以自定义请求的URL进行数据处理
                    var html = '<a href="box/address/unit/edit/ids/' + row.house_id + '" class="btn-dialog" title="编辑" data-table-id="table"><b>编辑</b></a> |';
                  
                    html += '<a class="text-danger btn-delme"  style="cursor:pointer" data-url="box/address/unit/delme" data-id="' + row.house_id + '"><b>删除</b></a>';

                    return html;
                }
            },
        }
    };
    return Controller;
});